<template>
  <el-container>
    <el-header>成绩登记</el-header>
    <!-- <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="学号">
        <el-input v-model="form.sno"></el-input>
      </el-form-item>

      <el-form-item label="课号">
        <el-input v-model="form.cno"></el-input>
      </el-form-item>

      <el-form-item label="成绩">
        <el-input v-model="form.grade"></el-input>
      </el-form-item>
    </el-form> -->
    <el-button type="primary" @click="addDialogVisible = true"
      >添加成绩</el-button
    >

    <el-form ref="form" :model="form" label-width="80px">
      <el-table :data="UserList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="学号" prop="sno"></el-table-column>
        <el-table-column label="姓名" prop="sname"></el-table-column>
        <el-table-column label="所在专业" prop="smajor"></el-table-column>
        <el-table-column label="年级" prop="syear"></el-table-column>
        <el-table-column label="课号" prop="cno"></el-table-column>
        <el-table-column label="课名" prop="cname"></el-table-column>
        <el-table-column label="成绩" prop="grade"></el-table-column>

        <el-table-column label="删除">
          <template slot-scope="scope">
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGrade(scope.row)"
            ></el-button>
          </template>
        </el-table-column>

        <el-table-column label="修改">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        title="添加成绩"
        :visible.sync="addDialogVisible"
        width="50%"
        @close="addDialogClosed"
      >
        <el-form
          :model="query"
          label-width="90px"
          :rules="addGradeRules"
          ref="addGradeRef"
        >
          <el-form-item label="学号" prop="sno">
            <el-input v-model="query.sno"></el-input>
          </el-form-item>

          <el-form-item label="课号" prop="cno">
            <el-input v-model="query.cno"></el-input>
          </el-form-item>

          <el-form-item label="成绩" prop="grade">
            <el-input v-model="query.grade"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="addGrade">确定</el-button>
          <el-button type="primary" @click="addDialogVisible = false"
            >取消</el-button
          >
        </span>
      </el-dialog>

      <el-dialog
        title="编辑成绩"
        :visible.sync="editDialogVisible"
        width="50%"
        @close="editDialogClosed"
      >
        <el-form :model="query" label-width="90px" ref="editGradeRef">
          <el-form-item label="学号" prop="sno">
            <el-input v-model="query.sno" disabled></el-input>
          </el-form-item>

          <el-form-item label="课号" prop="cno">
            <el-input v-model="query.cno" disabled></el-input>
          </el-form-item>

          <el-form-item label="成绩" prop="grade">
            <el-input v-model="query.grade"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="editGrade">确定</el-button>
          <el-button type="primary" @click="editDialogVisible = false"
            >取消</el-button
          >
        </span>
      </el-dialog>
    </el-form>
  </el-container>
</template>

<script>
export default {
  created() {
    this.getUserList();
  },
  data() {
    return {
      form: {},
      query: {
        sno: "",
        cno: "",
        grade: ""
      },
      addGradeRules: {
        grade: [{ required: true, message: "请输入成绩", trigger: "blur" }]
      },
      UserList: [],
      total: 0,
      addDialogVisible: false,
      editDialogVisible: false
    }
  },
  methods: {

    //接口调用后端数据
    async getUserList() {
      this.$axios.post("http://localhost:8081/abc", this.query).then(res => {
        console.log(res.data);
        this.UserList = res.data;
      });
    },
    addDialogClosed() {
      this.$refs.addGradeRef.resetFields();
    },
    async addGrade() {
      this.$refs.addGradeRef.validate(async valid => {
        if (!valid) return;
        this.$axios.post("http://localhost:8081/addGrade", this.query).then(res => {
          if (res != "success") {
            this.addDialogVisible = false;
            this.getUserList();
            return this.$message.success("操作成功");
          }
          this.$message.error("操作失败");
        });
      });
    },

    async deleteGrade(data) {
      const confirmResult = await this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult != 'confirm') {
        return this.$message.info("已取消删除");
      }
      //{ data: sno, cno }
      this.$axios.delete("http://localhost:8081/deleteGrade?sno=" + data.sno + "&cno=" + data.cno).then(res => {
        if (res != "success") {
          this.getUserList();
          return this.$message.success("删除成功");
        }
        this.$message.error("删除失败");
      })
    },

    async showEditDialog() {
      this.$axios.post("http://localhost:8081/abc").then(res => {
        this.query = res.data;
        this.editDialogVisible = true;
      })
    },
    editDialogClosed() {
      this.$refs.editGradeRef.resetFields();
    },
    async editGrade() {
      this.$refs.editGradeRef.validate(async valid => {
        if (!valid) return;
        this.$axios.post("http://localhost:8081/editGrade", this.query).then(res => {
          if (res != "success") {
            this.addDialogVisible = false;
            this.getUserList();
            return this.$message.success("操作成功");
          }
          this.$message.error("操作失败");
        })
      })
    }
  }
};
</script>

<style scoped>
.el-header {
  color: #333;
  line-height: 60px;
  font-size: 40px;
}
</style>